﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GovernmentOfficeProject.LevelBusinessLogic;
using GovernmentOfficeProject.LevelDataAccessModel;
using System.Collections;
using GovernmentOfficeProject.SysTools;
using GovernmentOfficeProject.LevelDataAccess;

namespace GovernmentOfficeProject.LevelViewUI.admin
{
    public partial class UserManagerDept : System.Web.UI.Page
    {
        decimal dclValueParameter = 0;
        BLL_USER userBLL = new BLL_USER();
        BLL_Department deptBLL = new BLL_Department();
        Entity_USER_User userEntity = null;
        List<Entity_USER_Department> userDeptList = null;
        List<Entity_USER_Department> allDeptList = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限判断
            if (SystemContext.CurrentUserID == 0 || SystemContext.IsAdmin == 0)
            {
                Response.Redirect("/LevelViewUI/admin/Login.htm?FromUrl=" + HttpUtility.UrlEncode(Request.Url.AbsoluteUri));
            }
            else
            {
                SystemContext.CurrentUserID = SystemContext.CurrentUserID;
                SystemContext.IsAdmin = 1;
            }
            if (Request.QueryString["val"]!=null)
            {
                dclValueParameter = Convert.ToDecimal(Request.QueryString["val"].Trim());
                Entity_USER_User userOBJ = new Entity_USER_User();
                userOBJ.UserID = dclValueParameter;
                userEntity = userBLL.getUserByUser(userOBJ);    //获取当前用户.
            }
            else
            {
                dclValueParameter = 0;
            }

            //需判断是修改部门(>0)还是用户新增加部门(==0).            
            if (!Page.IsPostBack)
            {
                if (dclValueParameter > 0)
                {                    
                    userDeptList = deptBLL.getDeptListByUser(userEntity);  //获取当前用户对应的部门实体列表.
                    allDeptList = deptBLL.getDeptList();
                    GridViewDeptSettingValues();
                }
                else if (dclValueParameter.Equals(0))
                {
                    allDeptList = deptBLL.getDeptList();
                    GridViewDept.DataSource = allDeptList;
                    GridViewDept.DataBind();
                }                                       
            }         
        }

        /// <summary>
        /// 为部门赋值: 给gridview赋值并判断部门的选中状态.
        /// </summary>
        public void GridViewDeptSettingValues()
        {
            //以Map的形式保存用户所在的部门，方便查找
            Dictionary<decimal, Entity_USER_Department> userDeptMap = new Dictionary<decimal, Entity_USER_Department>();
            foreach (Entity_USER_Department userDept in userDeptList)
            {
                userDeptMap.Add(userDept.DeptID, userDept);
            }
            foreach (Entity_USER_Department allDept in allDeptList)
            {
                if (userDeptMap.ContainsKey(allDept.DeptID))
                {
                    allDept.Selected = true;
                }
                else
                {
                    allDept.Selected = false;
                }
            }
            GridViewDept.DataSource = allDeptList;
            GridViewDept.DataBind();
        }


        /// <summary>
        /// 选中gridview中每行的chackbox,触发添加删除用户对应部门的操作.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ckbDeptSelect_CheckedChanged(object sender, EventArgs e)
        {
            List<Entity_USER_Department> deptList = null;

            if (((CheckBox)sender).Checked) //选中.
            {
                deptList = new List<Entity_USER_Department>();
                AppLog.Write("dcl=" + dclValueParameter.ToString(), AppLog.LogMessageType.Debug);
                userBLL.deleteUserDeptByUserID(userEntity.UserID);  //←先删除对应部门,再重新添加↓.
                foreach (GridViewRow row in GridViewDept.Rows)
                {
                    if (((CheckBox)row.FindControl("ckbDeptSelect")).Checked)
                    {
                        Entity_USER_Department deptEntity = new Entity_USER_Department();
                        deptEntity.DeptID = Convert.ToDecimal(GridViewDept.DataKeys[row.RowIndex].Value);
                        deptList.Add(deptEntity);
                    }
                }
                bool blAddState = userBLL.addUserAndDeptlist(userEntity, deptList);
                if (blAddState == true)
                {
                    //GridViewDeptSettingValues();
                }
            }
            else
            {
                foreach (GridViewRow row in GridViewDept.Rows)
                {
                    if (!((CheckBox)row.FindControl("ckbDeptSelect")).Checked)
                    {
                        decimal deptID = Convert.ToDecimal(GridViewDept.DataKeys[row.RowIndex].Value);  //获取部门ID.
                        userBLL.deleteUserDeptByUserIDAndDeptID(userEntity.UserID, deptID);
                    }
                }

            }
        }


#region 暂时没用

        //弹出的部门窗口--确定按钮操作
        protected void linkBtnDeptConfirm_Click(object sender, EventArgs e)
        {
            //Response.Redirect("UserManagerDetail.aspx?umduse=updateDeptED",false);
                        //if (flag)
            //{
            //lblOperationState.Text = "成功保存用户所属部门.";
            //}
        }

        //弹出的部门窗口--取消按钮操作
        protected void linkBtnDeptCancel_Click(object sender, EventArgs e)
        {
            //lblOperationState.Text = "已取消用户所属部门操作.";
        }
#endregion

    }
}
